﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using loveyou.Core;
using loveyou.Models.ViewData;

namespace loveyou.Controllers
{
    public class ClientAgentController : BaseController
    {
        //掩饰项目，这里暂时忽略管理员权限判断

        private List<IGrouping<string, WURFL.IDevice>> GetGroupData()
        {
            var agentGroup = GlobalConfig.OnlineUserList
                                      .Select(z => z.Value.DeviceInfo)
                                      .GroupBy(z => z.Id /*z.NormalizedUserAgent*/)
                                      .OrderByDescending(z => z.Count())
                                      .ToList();
            return agentGroup;
        }

        public ActionResult Index()
        {
            var agentGroup = GetGroupData();

            var data = agentGroup.Select(z => new
            {
                OS = z.Key,
                Devices = new
                {
                    Count = z.Count(),
                    List = z.Select(l => new
                    {
                        l.NormalizedUserAgent,
                        l.FallbackId
                    }).ToList()
                }
            }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Stat()
        {
            var agentGroup = GlobalConfig.OnlineUserList;
            return Json(agentGroup, JsonRequestBehavior.AllowGet);
        }

        public ActionResult List()
        {
            //TODO:完善列表

            var userInfoList = GlobalConfig.OnlineUserList.Select(z => z.Value).OrderByDescending(z => z.UserName).ToList();
            Client_ListVD vd = new Client_ListVD()
            {
                UserInfoList = userInfoList
            };
            return View(vd);
        }

        public ActionResult ChangeController(bool isController, string userName)
        {
            if (!Request.IsAjaxRequest())
            {
                return null;
            }

            var userInfo = GlobalConfig.OnlineUserList.ContainsKey(userName) ? GlobalConfig.OnlineUserList[userName] : null;
            if (userInfo != null)
            {
                userInfo.IsController = isController;
                return Content(isController ? "1" : "0");
            }
            else
            {
                return Content("-1");
            }
        }

        public ActionResult GetController(string userName)
        {
            if (!Request.IsAjaxRequest())
            {
                return null;
            }
            var userInfo = GlobalConfig.OnlineUserList.ContainsKey(userName) ? GlobalConfig.OnlineUserList[userName] : null;
            if (userInfo != null)
            {
                return Content(userInfo.IsController ? "1" : "0");
            }
            else
            {
                return Content("-1");
            }
        }
    }
}
